package paskal.ui.components;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.swing.JComponent;

import paskal.ui.Style;

public class Thumb implements Component, MouseListener {
	private BufferedImage icon = null;
	private BufferedImage rolloverIcon = null;
	private BufferedImage pressedIcon = null;
	private BufferedImage currentIcon = null;
	private int startSplitLine = 0;
	private int endSplitLine = 0;

	@Override
	public void stylize(String selector) {
		Style.getInstance().styleThisShit(selector, this);
	}

	@Override
	public void setProperty(String key, Object value) {
		switch (key) {
		// case "bounds": {
		// setBounds((Rectangle) value);
		// break;
		// }
		case "startSplitLine": {
			startSplitLine = (int) value;
			break;
		}
		case "endSplitLine": {
			endSplitLine = (int) value;
			break;
		}
		case "background": {
			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) value;
			Rectangle p = (Rectangle) background.get("position");
			if (p != null) {
				setIcon(Style.getInstance().getImage(
						(String) background.get("image"), p));
			} else {
				setIcon(Style.getInstance().getImage(
						(String) background.get("image")));
			}
			currentIcon = icon;
			break;
		}
		case "hover": {
			@SuppressWarnings("unchecked")
			Map<String, Object> hover = (Map<String, Object>) value;
			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) hover
					.get("background");
			Rectangle r = (Rectangle) background.get("position");
			if (r != null) {
				setRolloverIcon(Style.getInstance().getImage(
						(String) background.get("image"), r));
			} else {
				setRolloverIcon(Style.getInstance().getImage(
						(String) background.get("image")));
			}
			break;
		}
		case "pressed": {
			@SuppressWarnings("unchecked")
			Map<String, Object> pressed = (Map<String, Object>) value;
			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) pressed
					.get("background");
			Rectangle r = (Rectangle) background.get("position");
			if (r != null) {
				setPressedIcon(Style.getInstance().getImage(
						(String) background.get("image"), r));
			} else {
				setPressedIcon(Style.getInstance().getImage(
						(String) background.get("image")));
			}
			break;
		}
		default:
			break;
		}
	}

	public BufferedImage getCurrentIcon() {
		return currentIcon;
	}

	public BufferedImage[] getSplitedCurrentIcon() {
		BufferedImage start = currentIcon.getSubimage(0, 0,
				currentIcon.getWidth(), startSplitLine);
		BufferedImage middle = currentIcon.getSubimage(0,
				startSplitLine, currentIcon.getWidth(),
				endSplitLine - startSplitLine);
		BufferedImage end = currentIcon.getSubimage(0,
				endSplitLine, currentIcon.getWidth(),
				currentIcon.getHeight() - endSplitLine);
		return new BufferedImage[]{start, middle, end};
	}

	public void setIcon(BufferedImage icon) {
		this.icon = icon;
	}

	public void setRolloverIcon(BufferedImage rolloverIcon) {
		this.rolloverIcon = rolloverIcon;
	}

	public void setPressedIcon(BufferedImage pressedIcon) {
		this.pressedIcon = pressedIcon;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		currentIcon = rolloverIcon;
		((JComponent) e.getSource()).repaint();
	}

	@Override
	public void mouseExited(MouseEvent e) {
		currentIcon = icon;
		((JComponent) e.getSource()).repaint();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		currentIcon = pressedIcon;
		((JComponent) e.getSource()).repaint();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		currentIcon = icon;
		((JComponent) e.getSource()).repaint();
	}
}